package com.qijin.util;

import com.alibaba.fastjson.JSON;
import com.qijin.config.MpayConstant;
import com.qijin.notify.BankcardBindNotifyDto;
import com.qijin.notify.BankcardPrePayNotifyDto;
import com.qijin.notify.BaseNotifyDto;
import lombok.var;
import org.springframework.util.StringUtils;

public class NotifyVerfyUtil {


    /**
     * 验证转换工具
     */
    public static BaseNotifyDto notifyConversionUtil(String json) throws Exception {
        var jsonObject = JSON.parseObject(json);
        var checkSign = SHA256withRSAUtil.checksign(MpayConstant.pubKey, GenSignUtils.genSignData(jsonObject), jsonObject.getString("sign"));
        if (!checkSign) {
            throw new Exception("未能通过验签！");
        }
        var bank_code = jsonObject.getString("bank_code");
        if (!StringUtils.isEmpty(bank_code)) {
            return JSON.parseObject(json, BankcardPrePayNotifyDto.class);
        } else {
            return JSON.parseObject(json, BankcardBindNotifyDto.class);
        }
    }
}
